package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.State;
import org.cuatrovientos.erp.model.Stock;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * implementation of GenericDAOInterface
 * @author Enrico Baroni
 *
 */
public class StateDAO implements GenericInterfaceDAO {

	@Autowired
	 private SessionFactory sessionFactory;
	

	/**
	 * default constructor
	 */
	public StateDAO () {
		
	}
	
	
	
	/* 
	 * selects one state by Id
	 * @param id
	 * @return state
	 */
	 @Transactional(readOnly = true)
	public State selectById(int id) {
		 State state = (State) getSession().get(State.class, id);
	    return state;
	}

	/*
	 * retrieves all states from db
	 * @return List of states
	 */
	 @Transactional(readOnly = true)
	public List<State> selectAll() {
	 
	    List<State> states = getSession().createQuery("from state").list();
	    return states;
	}

	/*
	 * inserts a new state in database
	 * retrieves generated id and sets to state instance
	 * @param new state
	 */
	 @Transactional
	public void insert(State state) {
	    Integer id = (Integer) getSession().save(state);
	    state.setId(id);           
	}

	/*
	 * updates state
	 * @param state to update
	 */
	 @Transactional
	public void update(State state) {
		getSession().merge(state);
	}

	/*
	 * delete given state
	 * @param state to delete
	 */
	 @Transactional
	public void delete(State state) {
		getSession().delete(state);
	}



	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void insert(Object newInsert) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void update(Object future) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public Session getSession() {
		 return sessionFactory.getCurrentSession();
	}
}
